package com.ckzp.weixin;

import com.alibaba.fastjson.JSON;
import com.ckzp.core.util.DateUtil;
import com.ckzp.core.util.StringUtil;
import com.jfinal.kit.HttpKit;
import com.jfinal.log.Log;

import java.util.Date;
import java.util.Map;

/**
 * Created by 51594 on 2022/8/16.
 */
public class AccessTokenTool {


    private static final Log log = Log.getLog(AccessTokenTool.class);

    //公众号默认
    private static String ACCESS_TOKEN;
    private static String JSAPI_TICKET;
    private static Date LOAD_DATE = new Date();
    private static Date JSAPI_LOAD_DATE = new Date();
    //公众号2
    private static String ACCESS_TOKEN2;
    private static String JSAPI_TICKET2;
    private static Date LOAD_DATE2 = new Date();
    private static Date JSAPI_LOAD_DATE2 = new Date();
    //小程序
    private static String APP_ACCESS_TOKEN;
    private static String APP_JSAPI_TICKET;
    private static Date APP_LOAD_DATE = new Date();
    private static Date APP_JSAPI_LOAD_DATE = new Date();


    public static String getAccessToken() {
        if (ACCESS_TOKEN == null || ACCESS_TOKEN.length() < 10 || (new Date().after(DateUtil.getAddDate(LOAD_DATE, (long) (60 * 60 * 1000))))) {
            return LoadAccessToken();
        } else {
            return ACCESS_TOKEN;
        }
    }

    /**
     * 取小程序
     *
     * @return
     */
    public static String getAppAccessToken() {
        if (APP_ACCESS_TOKEN == null || APP_ACCESS_TOKEN.length() < 10 || (new Date().after(DateUtil.getAddDate(APP_LOAD_DATE, (long) (60 * 60 * 1000))))) {
            return LoadAppAccessToken();
        } else {
            return APP_ACCESS_TOKEN;
        }
    }


    public static String getAccessToken2() {
        if (ACCESS_TOKEN2 == null || ACCESS_TOKEN2.length() < 10 || (new Date().after(DateUtil.getAddDate(LOAD_DATE2, (long) (60 * 60 * 1000))))) {
            return LoadAccessToken2();
        } else {
            return ACCESS_TOKEN2;
        }
    }

    /**
     * @return
     */
    public static String LoadAccessToken() {
        String APPID = WxTool.getAppID();
        String SECRET = WxTool.getAppSecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
        String htmls = HttpKit.get(url);
        Map m = JSON.parseObject(htmls, Map.class);
        if (StringUtil.getInt(m, "errcode") > 0) {
            log.error("url:" + url);
            log.error("取token失败" + m.toString());
        }
        if (m != null) {
            String access_token = StringUtil.getString(m, "access_token");
            ACCESS_TOKEN = access_token;
            LOAD_DATE = new Date();
            return access_token;
        }
        return null;
    }


    /**
     * @return
     */
    public static String LoadAppAccessToken() {
        String APPID = WxTool.getAppAppID();
        String SECRET = WxTool.getAppAppSecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
        String htmls = HttpKit.get(url);
        Map m = JSON.parseObject(htmls, Map.class);
        if (m != null) {
            String access_token = StringUtil.getString(m, "access_token");
            APP_ACCESS_TOKEN = access_token;
            APP_LOAD_DATE = new Date();
            return access_token;
        }
        return null;
    }

    /**
     * @return
     */
    public static String LoadAccessToken2() {
        String APPID = WxTool.getAppID2();
        String SECRET = WxTool.getAppSecret2();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
        String htmls = HttpKit.get(url);
        Map m = JSON.parseObject(htmls, Map.class);
        if (m != null) {
            String access_token = StringUtil.getString(m, "access_token");
            ACCESS_TOKEN2 = access_token;
            LOAD_DATE2 = new Date();
            return access_token;
        }
        return null;
    }

    /**
     * @return
     */
    public static String getJsapiTicket() {
        if (JSAPI_TICKET == null || JSAPI_TICKET.length() < 10 || (new Date().after(DateUtil.getAddDate(JSAPI_LOAD_DATE, (long) (60 * 60 * 1000))))) {
            return LoadJsapiTicket();
        } else {
            return JSAPI_TICKET;
        }
    }

    /**
     * @return
     */
    public static String getJsapiTicket2() {
        if (JSAPI_TICKET2 == null || JSAPI_TICKET2.length() < 10 || (new Date().after(DateUtil.getAddDate(JSAPI_LOAD_DATE2, (long) (60 * 60 * 1000))))) {
            return LoadJsapiTicket2();
        } else {
            return JSAPI_TICKET2;
        }
    }

    /**
     * @return
     */
    public static String LoadJsapiTicket() {
        String ACCESS_TOKEN = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + ACCESS_TOKEN + "&type=jsapi";
        String htmls = HttpKit.get(url);
        Map m = JSON.parseObject(htmls, Map.class);
        if (m != null) {
            String ticket = StringUtil.getString(m, "ticket");
            JSAPI_TICKET = ticket;
            JSAPI_LOAD_DATE = new Date();
            return ticket;
        }
        return null;
    }

    /**
     * @return
     */
    public static String LoadJsapiTicket2() {
        String ACCESS_TOKEN = getAccessToken2();
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + ACCESS_TOKEN + "&type=jsapi";
        String htmls = HttpKit.get(url);
        Map m = JSON.parseObject(htmls, Map.class);
        if (m != null) {
            String ticket = StringUtil.getString(m, "ticket");
            JSAPI_TICKET2 = ticket;
            JSAPI_LOAD_DATE2 = new Date();
            return ticket;
        }
        return null;
    }
}
